#include "stdafx.h"
#include "YStream.h"

namespace ysimple_system
{
	YStream::YStream(void)
		: m_writerIndex(0)
		, m_readerIndex(0)
	{
	}


	YStream::~YStream(void)
	{
	}

	void YStream::write( const void* data, size_t len )
	{
		size_t tempWriterIndex = m_writerIndex + len;
		size_t copiableLen = len;		
		if (sizeof(m_buffer) < tempWriterIndex)
		{
			copiableLen = tempWriterIndex - sizeof(m_buffer);
		}
		
		memcpy(m_buffer+m_writerIndex, data, copiableLen);
		m_writerIndex += copiableLen;
	}

	void YStream::read( void* data, size_t len )
	{
		size_t tempReaderIndex = m_readerIndex + len;
		size_t copiableLen = len;
		if (m_writerIndex < tempReaderIndex)
		{
			copiableLen = tempReaderIndex - m_writerIndex;
		}

		memcpy(data, m_buffer+m_readerIndex, copiableLen);
		m_readerIndex += copiableLen;
	}

	char* YStream::getBuffer()
	{
		return m_buffer;
	}

	size_t YStream::len()
	{
		return m_writerIndex;
	}

	void YStream::release()
	{
		delete this;
	}

}

